/**
 * Created by TEANA on 2018/1/3.
 */
module.exports = ngModule => {
  ngModule.directive('compileHtml', function ($compile) {
    return {
      replace: true,
      restrict: 'EA',
      link: function (scope, elm, iAttrs) {
        let DUMMY_SCOPE = {
            $destroy: angular.noop
          },
          root = elm,
          childScope,
          destroyChildScope = function () {
            (childScope || DUMMY_SCOPE).$destroy();
          };
        iAttrs.$observe("html", function (html) {
          if (html) {
            destroyChildScope();
            childScope = scope.$new(false);
            var content = $compile(html)(childScope);
            root.replaceWith(content);
            root = content;
          }
          scope.$on("$destroy", destroyChildScope);
        });
      }
    };
  });
};
